#ifndef __GLF_TASK_TASKFUNC_H_INCLUDED__
#define __GLF_TASK_TASKFUNC_H_INCLUDED__

#include <glf/core/inlines.h>
#include <glf/task/task.h>
#include <glf/task/taskHandler.h>
#include <glf/task/taskDirector.h>
#include <glf/task/taskManager.h>

namespace glf {

// free functions
template<typename Tag1>
void DrainTask(bool wait = false)
{
	bool hasPendingTasks;
	TaskHandler<Tag1> th1;

	do
	{
		hasPendingTasks  = th1.Consume();
		hasPendingTasks |= TaskDirector::GetInstance().ConsumeRegisteredHandler();
		if(!hasPendingTasks && wait)
		{
			if(TaskManager::GetInstance<Tag1>().IsWorking())
			{
				hasPendingTasks = true;
				glf::Thread::Sleep(0);
			}
		}
	} 
	while(hasPendingTasks);
}

template<typename Tag1, typename Tag2>
void DrainTask(bool wait = false)
{
	bool hasPendingTasks;
	TaskHandler<Tag1> th1;
	TaskHandler<Tag2> th2;

	do
	{
		hasPendingTasks  = th1.Consume();
		hasPendingTasks |= th2.Consume();
		hasPendingTasks |= TaskDirector::GetInstance().ConsumeRegisteredHandler();
		if(!hasPendingTasks && wait)
		{
			if(TaskManager::GetInstance<Tag1>().IsWorking() 
				|| TaskManager::GetInstance<Tag2>().IsWorking())
			{
				hasPendingTasks = true;
				glf::Thread::Sleep(0);
			}
		}
	} 
	while(hasPendingTasks);
}

} // end namespace glf

#endif // __GLF_TASK_TASKFUNC_H_INCLUDED__
